package apy.codecase.base;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import apy.codecase.R;
import apy.codecase.business.view.activity.AddPswActivity;
import apy.codecase.model.event.MessageEvent;
import apy.codecase.utils.UIUtils;
import butterknife.ButterKnife;

/**
 * Created by apy on 2017/7/19.
 */

public abstract class BaseActivity<K, V> extends AppCompatActivity implements BaseView<K, V> {
    public BasePresenter presenter;
    //页面标题栏
    private RelativeLayout baseTitleRl;
    public View mView;
    private TextView baseTitleTv;
    public TextView baseTitleRight;
    public ImageView baseTitleRightIv;
    public FrameLayout mContainer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        EventBus.getDefault().register(this);
        baseTitleTv = (TextView) findViewById(R.id.base_title_tv);
        baseTitleRl = (RelativeLayout) findViewById(R.id.base_title_rl);
        mContainer = (FrameLayout) findViewById(R.id.base_container);
        baseTitleRight = (TextView) findViewById(R.id.base_title_right_tv);
        baseTitleRightIv = (ImageView) findViewById(R.id.base_title_right_Iv);
        mView = View.inflate(this, getViewId(), null);
        mContainer.addView(mView);

        ButterKnife.bind(this);
        setTitleTv();
        initData();
        controlTitleRl();

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public void controlTitleRl() {
        if (controlTitle()) {
            baseTitleRl.setVisibility(View.VISIBLE);
        } else {
            baseTitleRl.setVisibility(View.GONE);
        }
    }

    public void controlTitleRlIv() {
        baseTitleRightIv.setVisibility(View.VISIBLE);
        baseTitleRightIv.setImageResource(rightImg());
    }

    public int rightImg() {
        return 0;
    }


    public void controlTitleRight() {
        baseTitleRight.setVisibility(View.VISIBLE);
        setTitleRightTv();
    }

    public void setTitleRightTv() {
        baseTitleRight.setText(rightText());
    }

    /**
     * 设置标题栏标题
     * @return
     */
    public String rightText() {
        return "默认";
    }

    private void setTitleTv() {
        baseTitleTv.setText(title());
    }

    protected abstract String title();

    /**
     * 控制是否显示标题栏
     * @return
     */
    protected abstract boolean controlTitle();

    /**
     * 设置布局
     * @return
     */
    protected abstract int getViewId();

    /**
     * 加载数据
     */
    protected abstract void initData();

    public void back(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            finishAfterTransition();
        } else {
            finish();
        }
    }

    public void rightTvClick(View view) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {/* Do something */}

    @Override
    public void afterLoadDataAsObject(Object t) {

    }

    @Override
    public void afterLoadData(K t, V v) {

    }

    public static void startActivity() {
        Intent intent = new Intent(UIUtils.getContext(), AddPswActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        UIUtils.getContext().startActivity(intent);
    }

    @Override
    public void afterLoadData(K t) {

    }

}
